package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;
@Slf4j
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {

    @Autowired
    HospitalSetService hospitalSetService;

    @ApiOperation("获取全部信息")
    @GetMapping("/findAll")
    public Result findAllHospitalSet(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }
    @ApiOperation("分页获取信息")
    @PostMapping("/findPageHospSet/{current}/{limit}")
    //HospitalSetQueryVo封装对象的状态码类
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  //@RequestBody()使用json的格式来传递参数  required = false :请求体可以为null 要使用post请求
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> objectPage = new Page<HospitalSet>(current,limit);
        //获取实体类的属性
        //用于模糊查询
        String hosname = hospitalSetQueryVo.getHosname();
        //where hoscode=?
        String hoscode = hospitalSetQueryVo.getHoscode();
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname",hosname);
        }
        if (!StringUtils.isEmpty(hoscode)){
            wrapper.eq("hoscode",hoscode);
        }
        Page<HospitalSet> page = hospitalSetService.page(objectPage, wrapper);
        return Result.ok(page);
    }
    //添加医院设置
    @ApiOperation("添加医院设置信息")
    @PostMapping("/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        // 状态码status 1 可以使用 0不能使用
        hospitalSet.setStatus(1);
        //signKey签名密钥
        Random random = new Random();
        int i = random.nextInt(1000);
        String encrypt = MD5.encrypt(System.currentTimeMillis() + "" + i);
        hospitalSet.setSignKey(encrypt);
        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
    //id获取医院设置信息
    @ApiOperation("id获取医院信息")
    @GetMapping("/getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id){
        if (!StringUtils.isEmpty(id)){
            HospitalSet byId = hospitalSetService.getById(id);
            return Result.ok(byId);
        }
        return Result.fail();
    }
    //修改医院设置信息
    @ApiOperation("修改医院设置信息")
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean b = hospitalSetService.updateById(hospitalSet);
        return b ? Result.ok() :Result.fail();
    }
    //逻辑删除
    @ApiOperation("医院设置逻辑删除")
    @DeleteMapping("idremove/{id}")
    public Result idremove(@PathVariable Long id){
        log.info("删除得值{}",id);
        hospitalSetService.removeById(id);
        return Result.ok();
    }
    //批量删除
    @ApiOperation("批量删除医院信息")
    @DeleteMapping("batcRemove")
    public Result batcRemove(@RequestBody List<Long> idList){
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }
    //根据id获取信息，并修改所=锁得信息
    @ApiOperation("设置锁和解锁")
    @PostMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status){
        HospitalSet byId = hospitalSetService.getById(id);
        byId.setStatus(status);
        hospitalSetService.updateById(byId);
        return Result.ok();
    }
    //发送签名密钥
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet byId = hospitalSetService.getById(id);
        String signKey = byId.getSignKey();
        String hoscode = byId.getHoscode();
        //发送短信
        return Result.ok();

    }

}
